<?php
/**
 * 支付配置
 * @author wyb <[<email address>]>
 */
namespace Qwadmin\Controller;

class PaySettingController extends ComController {

	private $_model = null;
    
    public function _initialize() {
        parent::_initialize();
        $this->_model = M('payment_config');
    }

	/**
	 * 支付列表
	 */
	public function lists() {
		$where = array();
        $page = I('get.p') ? I('get.p') : 1;
        $data = $this->_model->where( $where )->page( $page , $this->_row )->order( 'id DESC' )->select();
        $total = $this->_model->where( $where )->count();
        $this->__assignPage( $total );
        $this->assign( 'lists' , $data );
        $this->display();
	}

	/**
	 * 增加支付配置
	 */
	public function add() {
		if( IS_POST ) {
			$data = I('post.poster') ? I('post.poster') : exit();
			if( $data['type'] == 1 ) {
				$data['info'] = $data['alipay'] ? serialize( $data['alipay'] ) : '';
			} else {
				$data['info'] = $data['weixin'] ? serialize( $data['weixin'] ) : '';
			}
			unset( $data['weixin'] );
			unset( $data['alipay'] );
			$data['create_time'] = time();
			if( $this->_model->add($data) === false ) {
				$this->error( '添加失败' );
			} else {
				$this->success( '添加成功' );
			}
		} else {
			$this->display();
		}
	}

	/**
	 * 修改支付配置
	 */
	public function save() {
		if( IS_POST ) {
			$data = I('post.poster') ? I('post.poster') : exit();
			if( $data['type'] == 1 ) {
				$data['info'] = $data['alipay'] ? serialize( $data['alipay'] ) : '';
			} else {
				$data['info'] = $data['weixin'] ? serialize( $data['weixin'] ) : '';
			}
			unset( $data['weixin'] );
			unset( $data['alipay'] );
			if( $this->_model->save($data) === false ) {
				$this->error( '修改失败' );
			} else {
				$this->success( '修改成功' );
			}
		} else {
			$payment = $this->_model->find( I('get.id') );
			if( !$payment ) $this->error( '错误' );
			$payment['info'] = $payment['info'] ? unserialize( $payment['info'] ) : '';
			$this->assign( 'info' , $payment );
			$this->display();
		}
	}

	/**
	 * 删除支付方式
	 */
	public function del() {
		$payment = $this->_model->find( I('get.id') );
		if( !$payment ) $this->error( '错误' );
		if( $this->_model->delete( I('get.id') ) ) {
			$this->success('删除成功');
		} else {
			$this->success('删除失败');
		}
	}
}